<?php
include_once '../header1.php';
?>
<script type="text/javascript">
function getHttpRequest()
{
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function showHint(str)
{
var xmlhttp;    
if (str=="")
  {
  document.getElementById("List").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   document.getElementById("List").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","sch_names.php?q="+str,true);
xmlhttp.send();
}
function fill(val)
{
document.form1.SchName.value=val;
document.getElementById("List").innerHTML="";
}
</script>
<script type="text/javascript">
function re_te()
{
document.getElementById("winner").innerHTML="Winner:</td><td><input type='text' name='Winner'/>";
}
function hide()
{
document.getElementById("winner").innerHTML="";
}

</script>
<link rel="stylesheet" href="../css/datepicker.css" type="text/css" />
    <link rel="stylesheet" media="screen" type="text/css" href="../css/layout.css" />
  	<script type="text/javascript" src="../js/jquery.js"></script>
	<script type="text/javascript" src="../js/datepicker.js"></script>
    <script type="text/javascript" src="../js/eye.js"></script>
    <script type="text/javascript" src="../js/utils.js"></script>
    <script type="text/javascript" src="../js/layout.js?ver=1.0.2"></script>
</head>
<body>
<?php
include_once ("../Login/databaseconnection.php");

if(!loggedin())
{
	require_once ("../index.php");
}
   
   else
   {
	date_default_timezone_set("Europe/Paris");
    $t_d=date("Y-m-d");
	if(isset($_POST['Register']))
{
	$Competition_Name=$_POST['Competition_Name'];
    $New_Competition=$_POST['New_Competition'];
	  $resultnet=mysql_query("SELECT * FROM competitions WHERE Competition_Name='$Competition_Name'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Competition_Id=$rownet['Competition_Id'];
}
    $SchName=$_POST['SchName'];
	$No_Of_Students=$_POST['No_Of_Students'];
    $No_Of_Teachers=$_POST['No_Of_Teachers'];
    $Result=$_POST['Result'];
	$Date=$_POST['Date'];
    $Scope=$_POST['Scope'];
	$Winner=$_POST['Winner'];
	 $name=explode(",",$SchName);
    $nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $School_Id=$row['School_Id'];
}
 if($SchName==""||($Competition_Name=="Select One"&&$New_Competition=="")||$Date=="")
	   {
	   $_SESSION['errorin']="All details are required Please fill all!";
	   }
	   if($School_Id!="Select One"&&($Competition_Name!="Select One"||$New_Competition!="")&&$Date!="")
	   {
		$Ex_Che=mysql_query("SELECT * FROM competition WHERE Competition_Id='$Competition_Id' AND School_Id='$School_Id' AND Date='$Date'");
		if(mysql_num_rows($Ex_Che)!=0)
		{
			$_SESSION['errorexi']="Sorry this Competition's record already exists";
		}
		else
		{
		if($_SESSION['Access_Type']=="Teacher in Charge")
{
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];  
   if($School_Id==$Own_School_Id)
	{
		require_once 'REG.php';
		}
		 else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
		}
		else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
		}
		else if($_SESSION['Access_Type']=="Administrator")
{
require_once 'REG.php';
}   
   else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
	}
	}
	}
?>
<?php
include_once "../header2.php";
?><?php
include_once "topmenu.php";
?>

<div id="content">
	<div id="main">
	  <div id="bodyfield">
      <div id="lefttablereg">
  <form name="form1" action="#" method="POST">
  <table width="451">
    <tr>
      <td height="23" colspan="7"  scope="row" class="err">
	  <?php
		if(isset($_POST['Register']))
{
	if(isset($_SESSION['errornotallo']))
{
echo '<img src="../images/alert.jpg" width="12" height="12"/>';
echo $_SESSION['errornotallo'];
unset($_SESSION['errornotallo']);
}
	if(isset($_SESSION['errorin']))
	{
	echo '<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['errorin'];
		unset($_SESSION['errorin']);
	}
	if(isset($_SESSION['error']))
	{
	echo '<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['error'];
		unset($_SESSION['error']);
		
	}
	if(isset($_SESSION['errorexi']))
	{
	echo '<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['errorexi'];
		unset($_SESSION['errorexi']);
		
	}
}
		?></td>
      </tr>
  </table>
  <table width="744" height="296" border="0" class="tablefontv" >
  
    <tr>
      <th width="8" rowspan="10"  scope="row">&nbsp;</th>
      <th width="10" height="30"  scope="row"><font color="red" size="+2"><p align="center">*</p></font></th>
      <th width="145"  scope="row">Competition Name:</th>
       <td>
  <select name="Competition_Name" id="Competition_Name">
        <?php
		 if(isset($_POST['Register']))
	  {
	  $resultnet=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$Competition_Id'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Competition_Name=$rownet['Competition_Name'];
}
	 echo '<option value="'.$Competition_Name.'">'.$Competition_Name.'</option>';
	  }
	  else
	  {
	  echo '<option value="Select One">Select One</option>';
	  }
	  	  $com=mysql_query("SELECT DISTINCT(Competition_Id) FROM competitions");
	  
  while($row=mysql_fetch_array($com))
  {
	 $Compe_Id=$row['Competition_Id'];
	 $resultnet=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$Compe_Id'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Compe_Name=$rownet['Competition_Name'];
}
if($Compe_Name!=$Competition_Name)
{
	  echo '<option value="'.$Compe_Name.'">'.$Compe_Name.'</option>';
}
  }
 
	  ?>
      </select>
	  </td><td><label><font color="red">or</font> New competition:&nbsp;&nbsp;&nbsp;</label>
	  <?php
	  if($New_Competition!="")
	  {
	  echo "<input type='text' name='New_Competition' value='$New_Competition'/>";
	  }
	  else
	  {
	  echo "<input type='text' name='New_Competition'/>";
	  }
	  ?>
	  </td>
          </tr>
      <tr>
      <th height="26" scope="row"><font color="red" size="+2"><p align="center">*</p></font></th>
      <th height="26" scope="row">School's Name:</th>
     <td colspan="2">
     <?php
	 	if($_SESSION['Access_Type']=="Teacher in Charge")
{
	$NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id']; 
    $snam=mysql_query("SELECT * FROM school WHERE School_Id='$Own_School_Id'");
	  if($ros=mysql_fetch_array($snam))
	  {
	  $SchName=$ros['Name'].','.$ros['Address'];
	  }
}
	 echo "<input type='text' id='SchName' name='SchName' onKeyUp='showHint(this.value)' value='$SchName' onClick='cleartbox()' size='40' readonly='readonly'/>";
}
	else if($_SESSION['Access_Type']=="Administrator")
{
	    echo "<input type='text' id='SchName' name='SchName' onKeyUp='showHint(this.value)' value='$SchName' onClick='cleartbox()' size='40'/>";
}
	  ?>
      
     </td>
      </tr>
    <tr>
      <th height="22" scope="row"><font color="red" size="+2"><p align="center">*</p></font></th>
      <th height="22" scope="row">Number Of Students: </th>
    
	    <?php
	   if(isset($_POST['Register']))
{
echo '<td>';
echo "<input type='text' name='No_Of_Students' value='$No_Of_Students'/>";
echo '</td>';

}
else
{
echo '<td>';
echo "<input type='text' name='No_Of_Students'/>";
echo '</td>';

}?>
       <td colspan="2">&nbsp;</td>
      </tr>
    <tr>
      <th height="30" scope="row"><font color="red" size="+2"><p align="center">*</p></font></th>
      <th height="30" scope="row">Number Of Teachers:</th>
      
	    <?php
	   if(isset($_POST['Register']))
{
echo '<td>';
echo "<input type='text' name='No_Of_Teachers' value='$No_Of_Teachers'/>";
echo '</td>';

}
else
{
echo '<td>';
echo "<input type='text' name='No_Of_Teachers'/>";
echo '</td>';

}?> <td colspan="2">&nbsp;</td>
      </tr>
    <tr>
      <th height="26" scope="row"><font color="red" size="+2"><p align="center">*</p></font></th>
      <th height="26" scope="row">Result: </th>
        <td width="136">
        <?php
	   if(isset($_POST['Register']))
{
	if($Result=="Win")
	  {
		echo '<input type="radio" name="Result" value="Win" id="Result" checked="checked" onClick="re_te()"/>';
	   echo "Win";
	  }
	  else
	  {
		  echo '<input type="radio" name="Result" value="Win" id="Result" onClick="re_te()"/>';
	   echo "Win";
	  }
        echo '<br />';
        if($Result=="Loss")
		{ 
		
          echo '<input type="radio" name="Result" value="Loss" id="Result" checked="checked" onClick="hide()"/>';
          echo "No";
  }
  else
  {
	   echo '<input type="radio" name="Result" value="Loss" id="Result" onClick="hide()"/>';
          echo "No";
  }
  }
  else
  {
  ?>
	<label>
	     <input type="radio" name="Result" value="Win" onClick="re_te()"/>
      Win</label>
      <br />
      <label>
       <input type="radio" name="Result" value="Loss" onClick="hide()"/>
        Loss</label>
		<?php
		}
		?>
        </td>
                    <?php
		if($Result=="Win")
		{
echo '<th>Winner:</th>';
	   if(isset($_POST['Register']))
{
echo '<td>';
echo "<input type='text' name='Winner' value='$Winner'/>";
echo '</td>';
}
else
{
echo '<td>';
echo "<input type='text' name='Winner'/>";
echo '</td>';
}
		}?>
        <td width="423"><div id="winner">
	     </div></td>
	      </tr>
    <tr>
      <th height="22" scope="row"><font color="red" size="+2"><p align="center">*</p></font></th>
      <th height="22" scope="row">Date: </th>
      
	    <?php
	   if(isset($_POST['Register']))
{
echo '<td>';
echo "<input type='text' name='Date' value='$Date' class='inputDate' id='inputDate'>";
echo '</td>';

}
else
{
echo '<td>';
echo "<input type='text' name='Date' value='$t_d' class='inputDate' id='inputDate'>";
echo '</td>';

}?> <td colspan="2">&nbsp;</td>
      </tr>
    <tr>
      <th height="22" scope="row"><font color="red" size="+2"><p align="center">*</p></font></th>
      <th height="22" scope="row">Scope: </th>
      
	    <?php
	   if(isset($_POST['Register']))
{
echo '<td>';
echo "<input type='text' name='Scope' value='$Scope'/>";
echo '</td>';

}
else
{
echo '<td>';
echo "<input type='text' name='Scope'/>";
echo '</td>';

}?> <td colspan="2">&nbsp;</td>
      </tr>
	  <tr>
	    <th height="24" colspan="4" scope="row" class="err"></th>
 
	  </tr>
  </table>
  
  <br/>
  <a href="RegCom.php" class="reset">Reset |</a>
  <input name="Register" align="middle" type="submit" class="buttonok" value="Register" />
 </form>
 </div>
 <div id="righttablereg">
 <?php
 if($_SESSION['OK']!="OK")
{
?>
 <div class="suggestionListsch" id="List"></div>
<?php
}
		if(isset($_POST['Register']))
{
if($_SESSION['OK']=="OK")
{
if($_SESSION['Access_Type']=="Teacher in Charge")
{
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
   $result=mysql_query("SELECT * FROM competition WHERE School_Id='$Own_School_Id'");
echo '<br/>';
if(mysql_num_rows($result)!=0)
   {
echo '<div style="width:550px; overflow-X:scroll; ">';
echo '<table border="1" class="tablefontv" >
<tr bgcolor="#6AB5FF">
<td><font color="#000000">Competition Id</font></td>
<td><font color="#000000">Date</font></td>
<td><font color="#000000">School Name</font></td>
<td><font color="#000000">Competition Name</font></td>
<td><font color="#000000">Number of students</font></td>
<td><font color="#000000">Number of Teachers</font></td>
<td><font color="#000000">Result</font></td>
<td><font color="#000000">Scope</font></td>
<td><font color="#000000">Winner</font></td>
<td><font color="#000000">Last Updated Date</font></td>
</tr>';

  while($row=mysql_fetch_array($result))
  {
	$Com_Id=$_SESSION['Competition_Id'];
	$Date=$_SESSION['Date'];
	$Sch_Id=$_SESSION['School_Id'];
		if($row['Competition_Id']=="$Com_Id"&&$row['Date']=="$Date"&&$row['School_Id']=="$Sch_Id")
	{
			$Competition_Id=$row['Competition_Id'];
		$res=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$Competition_Id'");
		if($rowc=mysql_fetch_array($res))
		{
			$Competition_Name=$rowc['Competition_Name'];
		}
		 $get_Sch_name=mysql_query("SELECT * FROM school WHERE School_Id='".$row['School_Id']."'");
if($rs=mysql_fetch_array($get_Sch_name))
{
$Name=$rs['Name'];
}
			echo '<tr bgcolor="#936">';
			echo '<td><font color="#000000">'.$row['Competition_Id'].'</font></td>';
			echo '<td><font color="#000000">'.$row['Date'].'</font></td>';
			echo '<td><font color="#000000">'.$Name.'</font></td>';
			echo '<td><font color="#000000">'.$Competition_Name.'</font></td>';
			echo '<td><font color="#000000">'.$row['No_Of_Students'].'</font></td>';
			echo '<td><font color="#000000">'.$row['No_Of_Teachers'].'</font></td>';
			echo '<td><font color="#000000">'.$row['Result'].'</font></td>';
			echo '<td><font color="#000000">'.$row['Scope'].'</td>';
			echo '<td><font color="#000000">'.$row['Winner'].'</td>';
			echo '<td><font color="#000000">'.$row['Last_Updated_Date'].'</font></td>';
			echo '</tr>';
		unset($_SESSION['Competition_Id']);
		unset($_SESSION['Date']);
		unset($_SESSION['School_Id']);

	}
	else
	{
		$Competition_Id=$row['Competition_Id'];
		$res=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$Competition_Id'");
		if($rowc=mysql_fetch_array($res))
		{
			$Competition_Name=$rowc['Competition_Name'];
		}
		$get_Sch_name=mysql_query("SELECT * FROM school WHERE School_Id='".$row['School_Id']."'");
if($rs=mysql_fetch_array($get_Sch_name))
{
$Name=$rs['Name'];
}
		echo '<tr bgcolor="#CCCCCC">';
		echo '<td>'.$row['Competition_Id'].'</td>';
		echo '<td>'.$row['Date'].'</td>';
		echo '<td>'.$Name.'</td>';
		echo '<td>'.$Competition_Name.'</td>';
		echo '<td>'.$row['No_Of_Students'].'</td>';
		echo '<td>'.$row['No_Of_Teachers'].'</td>';
		echo '<td>'.$row['Result'].'</td>';
		echo '<td>'.$row['Scope'].'</td>';
		echo '<td>'.$row['Winner'].'</td>';
		echo '<td>'.$row['Last_Updated_Date'].'</td>';
		echo '</tr>';
	}
  }
   echo "</table>";
echo '</div>';
  }
   }
   }
  else if($_SESSION['Access_Type']=="Administrator")
{ 
$result=mysql_query("SELECT * FROM competition");
echo '<br/>';
if(mysql_num_rows($result)!=0)
   {
echo '<div style="width:550px; overflow-X:scroll; ">';
echo '<table width="208" border="1" class="tablefontv" >
<tr bgcolor="#6AB5FF">
<td><font color="#000000">Competition Id</font></td>
<td><font color="#000000">Date</font></td>
<td><font color="#000000">School Name</font></td>
<td><font color="#000000">Competition Name</font></td>
<td><font color="#000000">Number of students</font></td>
<td><font color="#000000">Number of Teachers</font></td>
<td><font color="#000000">Result</font></td>
<td><font color="#000000">Scope</font></td>
<td><font color="#000000">Winner</font></td>
<td><font color="#000000">Last Updated Date</font></td>
</tr>';

 while($row=mysql_fetch_array($result))
  {
	$Competition_Id=$_SESSION['Competition_Id'];
	$Date=$_SESSION['Date'];
	$School_Id=$_SESSION['School_Id'];
	if($row['Competition_Id']==$Competition_Id&&$row['Date']==$Date&&$row['School_Id']==$School_Id)
	{
		 $Competition_Id=$row['Competition_Id'];
		$res=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$Competition_Id'");
		if($rowc=mysql_fetch_array($res))
		{
			$Competition_Name=$rowc['Competition_Name'];
		}
		$get_Sch_name=mysql_query("SELECT * FROM school WHERE School_Id='".$row['School_Id']."'");
if($rs=mysql_fetch_array($get_Sch_name))
{
$Name=$rs['Name'];
}
			echo '<tr bgcolor="#936">';
			echo '<td><font color="#000000">'.$row['Competition_Id'].'</font></td>';
			echo '<td><font color="#000000">'.$row['Date'].'</font></td>';
			echo '<td><font color="#000000">'.$Name.'</font></td>';
			echo '<td><font color="#000000">'.$Competition_Name.'</font></td>';
			echo '<td><font color="#000000">'.$row['No_Of_Students'].'</font></td>';
			echo '<td><font color="#000000">'.$row['No_Of_Teachers'].'</font></td>';
			echo '<td><font color="#000000">'.$row['Result'].'</font></td>';
			echo '<td><font color="#000000">'.$row['Scope'].'</td>';
			echo '<td><font color="#000000">'.$row['Winner'].'</td>';
			echo '<td><font color="#000000">'.$row['Last_Updated_Date'].'</font></td>';
			echo '</tr>';
		unset($_SESSION['Competition_Id']);
		unset($_SESSION['Date']);
		unset($_SESSION['School_Id']);

	}
	else
	{
		$Competition_Id=$row['Competition_Id'];
		$res=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$Competition_Id'");
		if($rowc=mysql_fetch_array($res))
		{
			$Competition_Name=$rowc['Competition_Name'];
		}
		$get_Sch_name=mysql_query("SELECT * FROM school WHERE School_Id='".$row['School_Id']."'");
if($rs=mysql_fetch_array($get_Sch_name))
{
$Name=$rs['Name'];
}
		echo '<tr bgcolor="#CCCCCC">';
		echo '<td>'.$row['Competition_Id'].'</td>';
		echo '<td>'.$row['Date'].'</td>';
		echo '<td>'.$Name.'</td>';
		echo '<td>'.$Competition_Name.'</td>';
		echo '<td>'.$row['No_Of_Students'].'</td>';
		echo '<td>'.$row['No_Of_Teachers'].'</td>';
		echo '<td>'.$row['Result'].'</td>';
		echo '<td>'.$row['Scope'].'</td>';
		echo '<td>'.$row['Winner'].'</td>';
		echo '<td>'.$row['Last_Updated_Date'].'</td>';
		echo '</tr>';
	}
  }
   echo "</table>";
   echo '</div>';
  }  
  }
  unset($_SESSION['OK']);
  }
  }
  ?>
  
 </div>
</div>
</div>
</div>
<div id="sidebar"><br/>
<div id="partners" class="boxed">
			<h2 class="title">COMPETITIONS</h2>
            </div>
  <?php
include_once "menu.php";
?>

<?php
include_once "../footer.php";
?>

<?php

	}
?>
</body>
</html>